package com.ruoyi.app.api.repository

import android.content.Context
import com.drake.net.Get
import com.drake.net.Post
import com.ruoyi.app.api.ConfigApi
import com.ruoyi.app.api.OKHttpUtils
import com.ruoyi.app.model.entity.CaptchaImageEntity
import com.ruoyi.app.model.entity.LoginEntity
import com.ruoyi.app.model.entity.MineEntity
import com.ruoyi.app.model.entity.WorkIndexEntity
import com.ruoyi.app.model.request.LoginRequest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.RequestBody

class AuthRepository(
    private var context: Context
) : AuthRepoInterface {

    companion object {
        private const val TAG = "AuthRepository"
    }

    override suspend fun getVerificationCode() = withContext(Dispatchers.IO) {
        Get<CaptchaImageEntity>(ConfigApi.getCaptchaImage).await()
    }

    override suspend fun login(
        validateCode: String,
        username: String,
        password: String,
        uuid: String?
    ): LoginEntity = withContext(Dispatchers.IO) {
        Post<LoginEntity>(ConfigApi.login) {
            body = OKHttpUtils.getRequestBody(
                LoginRequest(
                    validateCode,
                    password,
                    username,
                    uuid ?: ""
                )
            )
        }.await()
    }

    override suspend fun getUserInfo(): MineEntity = withContext(Dispatchers.IO) {
        Get<MineEntity>(ConfigApi.getInfo).await()
    }

    override suspend fun getWorkData(): WorkIndexEntity = withContext(Dispatchers.IO) {
        Get<WorkIndexEntity>(ConfigApi.appWork).await()
    }

    override suspend fun register(requestBody: RequestBody): LoginEntity =
        withContext(Dispatchers.IO) {
            Post<LoginEntity>(ConfigApi.register) {
                body = requestBody
            }.await()
        }
}

